//
//  MTClubActivitiesViewCell.swift
//  MT
//
//  Created by ooops on 2019/12/26.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTClubActivitiesViewCell: UITableViewCell {
    
    class func reuseIdentity() -> String {
        return "MTClubActivitiesViewCellID"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        selectionStyle = .none
        backgroundColor = UIColor.theme.background
        
        
//        activitiesNameLabel.attributedText = arrtibute(text: "  周末钓个鱼", icon: "icon_huodong", position: 0)
//        activitiesDateLabel.attributedText = arrtibute(text: "  2019-01-01", icon: "icon_time", position: 0)
//        activitiesAddressLabel.attributedText = arrtibute(text: "  上海市长宁区北翟路110号", icon: "icon_location", position: 0)
//        detailLabel.attributedText = arrtibute(text: "查看详情", icon: "icon_arrow_right", position: 1, textColor: UIColor.redpack.x5d5d5d)
    }
    
    var model: MTClubActivitModel? {
        didSet {
            
            if let name = model?.name {
                activitiesNameLabel.attributedText = arrtibute(text: "  \(name)", icon: "icon_huodong", position: 0)
            }
            
            if let date = model?.startTime {
                //let dateS = DateManager.string(date: Date, format: <#T##String?#>)
                if let value = DateManager.date(dateString: date, format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ") {
                    let dateString = DateManager.string(date: value, format: "yyyy-MM-dd")
                    activitiesDateLabel.attributedText = arrtibute(text: "  \(dateString)", icon: "icon_time", position: 0)
                }
            }
            
            if let address = model?.address {
                activitiesAddressLabel.attributedText = arrtibute(text: "  \(address)", icon: "icon_location", position: 0)
            }
            
            if let join = model?.enrollNum, let num = model?.num {
                leftJoinActLabel.text = "\(join) / \(num)"
            }
            
            detailLabel.attributedText = arrtibute(text: "查看详情", icon: "icon_arrow_right", position: 1, textColor: UIColor.redpack.x5d5d5d)
            
            
            
            if let num = model?.enrollNum, let join = model?.num {
                joinAvtivitiesLabel.text = (num == join) ? "人数已满" : model?.joined == true ? "已报名" : "去报名"
            }
            
            if let status = model?.activityStatus {
//                if let date = model?.startTime {
//                    if let value = DateManager.date(dateString: date, format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ") {
//                        let compose = value.compare(Date())
//                        if compose == .orderedAscending {
//                            bottomActionView.isHidden = true
//                            avtivitiesOverLabel.isHidden = false
//                        }
//                    }
//                } else
                if status == 2 || status == 3 {
                    bottomActionView.isHidden = true
                    avtivitiesOverLabel.isHidden = false
                } else {
                    bottomActionView.isHidden = false
                    avtivitiesOverLabel.isHidden = true
                }
            }
            
            if let nickName = model?.nickname {
                nameLabel.text = nickName
            }
            
            if let href = model?.href {
                avatorImageView.kf.setImage(with: URL(string: href))
            }
            
        }
    }
    
    private func setupUI() {
        contentView.addSubview(bgColorView)
        bgColorView.addSubview(avatorImageView)
        bgColorView.addSubview(nameLabel)
        bgColorView.addSubview(detailLabel)
        
        bgColorView.addSubview(activitiesNameLabel)
        bgColorView.addSubview(activitiesDateLabel)
        bgColorView.addSubview(activitiesAddressLabel)
        
        bgColorView.addSubview(bottomActionView)
        bgColorView.addSubview(avtivitiesOverLabel)
        
        bottomActionView.addSubview(leftJoinActLabel)
        bottomActionView.addSubview(lineView)
        bottomActionView.addSubview(joinAvtivitiesLabel)
    }
    
    private func makeConstraint() {
        bgColorView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-24)
        }
        
        avatorImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(14)
            make.top.equalToSuperview().offset(9)
            make.width.height.equalTo(22)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatorImageView.snp.right).offset(4)
            make.centerY.equalTo(avatorImageView)
            make.right.equalTo(detailLabel.snp.left)
        }
        
        detailLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.right.equalToSuperview().offset(-16)
        }
        
        activitiesNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-16)
        }
        
        activitiesDateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(activitiesNameLabel.snp.bottom).offset(4)
            make.right.equalToSuperview().offset(-16)
        }
        
        activitiesAddressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(activitiesDateLabel.snp.bottom).offset(4)
            make.right.equalToSuperview().offset(-16)
        }
        
        bottomActionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(activitiesAddressLabel.snp.bottom).offset(20)
            make.bottom.equalToSuperview()
            make.height.equalTo(40).priority(.high)
        }
        
        leftJoinActLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        joinAvtivitiesLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(16)
            make.width.equalTo(1)
        }
        
        avtivitiesOverLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(activitiesAddressLabel.snp.bottom).offset(20)
            make.bottom.equalToSuperview()
            make.height.equalTo(40).priority(.high)
        }
        
    }
    
    
    lazy var bgColorView: UIView = {
        let v = UIView()
        v.backgroundColor = .black
        v.layer.cornerRadius = 10
        return v
    }()
    
    lazy var avatorImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 11
        imageV.layer.masksToBounds = true
        imageV.backgroundColor = .white
        imageV.contentMode = .scaleAspectFill
        return imageV
    }()
    
    
    lazy var nameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.redpack.x5d5d5d
        l.text = "Nina发起的"
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    lazy var detailLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.redpack.x5d5d5d
        l.text = "查看详情"
        l.textAlignment = .right
        return l
    }()
    
    lazy var activitiesNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(16)
        l.textColor = .white
        l.text = "周末钓个鱼"
        return l
    }()
    
    lazy var activitiesDateLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(16)
        l.textColor = .white
        l.text = "2019-01-01"
        return l
    }()
    
    lazy var activitiesAddressLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(16)
        l.textColor = .white
        l.text = "上海市长宁区北翟路110号"
        return l
    }()
    
    lazy var bottomActionView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.theme.main
        v.layer.cornerRadius = 5
        return v
    }()
    
    lazy var leftJoinActLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = .black
        l.text = "1000 / 290"
        l.textAlignment = .center
        return l
    }()
    
    lazy var joinAvtivitiesLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = .black
        l.text = "去报名"
        l.textAlignment = .center
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = .black
        return v
    }()
    
    lazy var avtivitiesOverLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = .black
        l.text = "活动已结束"
        l.backgroundColor = UIColor.redpack.x5d5d5d
        l.layer.cornerRadius = 5
        l.layer.masksToBounds = true
        l.textAlignment = .center
        l.isHidden = true
        return l
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    
    private func arrtibute(text: String, icon: String, position: Int, iconSize: CGSize? = CGSize(width: 15, height: 15), iconYFix: CGFloat = -2, textColor: UIColor = UIColor(hex: 0xffffff)) -> NSAttributedString {
        
        let markAttribute = NSMutableAttributedString(string: text)
        
        let arrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : textColor,
                                                             NSAttributedString.Key.font : PingFangSCMediumFont(16)]
        
        markAttribute.addAttributes(arrtibuteValue, range: NSRange(location: 0, length: text.count))
        
        let markattch = NSTextAttachment()
        
        markattch.image = UIImage(named: icon)
        
        markattch.bounds = CGRect(x: 0, y: iconYFix, width: iconSize!.width, height: iconSize!.height)
        
        let markattchStr = NSAttributedString(attachment: markattch)
        
        /// icon 插入的位置
        if position == 0 {
            markAttribute.insert(markattchStr, at: 0)
        } else {
            markAttribute.append(markattchStr)
        }
        
        return markAttribute
    
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

//class CPSPFMarkLabel: UILabel {
//
//    convenience init(text: String) {
//        self.init(frame:CGRect.zero)
//
//        let markLabelText = " 清明时节雨纷纷☺" + text
//        self.textAlignment = .center
//        self.textColor = UIConstants.AppFontColor
//        self.font = UIConstants.DefaultFont15
//        self.backgroundColor = UIConstants.AppWhiteColor
//        let markAttribute = NSMutableAttributedString(string: markLabelText)
//        markAttribute.addAttribute(NSAttributedStringKey.foregroundColor, value:UIConstants.AppRedColor, range: NSRange(location: 6,length: text.count))
//        //以上是富文本显示
//        let arkattch = NSTextAttachment() //定义一个attachment
//        markattch.image = UIImage(named: "image_timing")//初始化图片
//        markattch.bounds = CGRect(x: 0, y: -1, width: 15, height: 15) //初始化图片的 bounds
//        let markattchStr = NSAttributedString(attachment: markattch) // 将attachment  加入富文本中
//        markAttribute.insert(markattchStr, at: 0)// 将markattchStr  加入原有文字的某个位置
//        self.attributedText = markAttribute
//    }
//}
